package com.gewei.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.gewei.dao.MenuDao;
import com.gewei.dao.PermissionDao;
import com.gewei.dao.RoleDao;
import com.gewei.entity.PageResult;
import com.gewei.entity.QueryPageBean;
import com.gewei.pojo.Role;
import com.gewei.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

   @Autowired
   private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        if (permissionIds !=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer>map =new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        if (menuIds !=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer>map =new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role>page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findpermissionIdsByroleId(Integer id) {
        return roleDao.findpermissionIdsByroleId(id);
    }

    @Override
    public List<Integer> findmenuIdsByroleId(Integer id) {
        return roleDao.findmenuIdsByroleId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit(role);
        roleDao.deleteAssocication1(role.getId());
        roleDao.deleteAssocication2(role.getId());
        Integer roleId = role.getId();
        if (permissionIds !=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer>map =new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        if (menuIds !=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer>map =new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        // 直接将这两个先从数据库删除，没有走 service 了
        permissionDao.deleteById(id);
        menuDao.deleteById(id);
        roleDao.deleteById(id);
    }
}
